//
//  HZGoodsColViewCell.m
//  app
//
//  Created by 刘华舟 on 15/5/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsColViewCell.h"
#import "HZGoodsModel.h"

#import "UIImageView+URL.h"
#import "UIFont+Conveniently.h"



@interface HZGoodsColViewCell()

@property (strong, nonatomic) UIImageView *goodsImgView;
@property (strong, nonatomic) UILabel *goodsTitleLabel;
@property (strong, nonatomic) UILabel *goodsPriceLabel;
@property (strong, nonatomic) UILabel *tagLabel;

@property (strong, nonatomic) UILabel *temaiLabel;

@end

@implementation HZGoodsColViewCell

//- (void)drawRect:(CGRect)rect{
//    //[self drawHorizonLineLeft:[self.ubo ux:kHZGoodsColViewMargin] right:[self.ubo uw:kHZGoodsColViewMargin] at:[self.ubo uh:kHZGoodsColViewWidth+kHZGoodsColLabelHeight +2*kHZGoodsColViewMargin]];
//    [self drawBackground:rect color:HZ_MALL_MAIN_THEME_WHITE];
//}

+ (instancetype)cellWithCollectionView:(HZCollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath{
    NSString *cellReuseIdentifier = @"HZGoodsColViewCell";
    HZGoodsColViewCell * cell= (HZGoodsColViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        NSLog(@"创建HZGoodsColViewCell失败");
    }
    return cell;
}

- (instancetype)initWithFrame:(CGRect)frame{
    frame = CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo uw:kHZGoodsColViewWidth uh:kHZGoodsColViewHeight]);
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = HZ_MALL_MAIN_THEME_WHITE;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = [self.ubo uw:8];
        self.layer.borderWidth = [self.ubo pw:2];
        self.layer.borderColor = HZ_MALL_SEPARATOR_CELL_LINE.CGColor;
        
        //图片
        self.goodsImgView = [[UIImageView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                        [self.ubo uw:kHZGoodsColViewWidth uh:kHZGoodsColViewWidth])];
        self.goodsImgView.backgroundColor = HZ_WHITE_COLOR;
//        self.goodsImgView.layer.masksToBounds = YES;
//        self.goodsImgView.layer.cornerRadius = [self.ubo uw:8];
        [self addSubview:self.goodsImgView];
        
        //标题
        self.goodsTitleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsImgView.ufo fx:UB_LEFT ux:2*kHZGoodsColViewMargin fy:UB_BOTTOM uy:2*kHZGoodsColViewMargin],
                                                                       [self.ubo fw:UB_WIDTH uw:-4*kHZGoodsColViewMargin uh:kHZGoodsColLabelHeight])];
        self.goodsTitleLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsTitleLabel setTextColor:HZ_MALL_TEXT_BLACK_COLOR];
        self.goodsTitleLabel.textAlignment = NSTextAlignmentLeft;
        self.goodsTitleLabel.numberOfLines = 2;
        [self.goodsTitleLabel setFont:kHZRFont_Content_L(kHZGoodsTabFontRateSix, kHZGoodsTabFontRatePlus)];
        [self addSubview:self.goodsTitleLabel];
        
        //价格
        self.goodsPriceLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsTitleLabel.ufo fx:UB_LEFT fy:UB_BOTTOM uy:2*kHZGoodsColViewMargin],
                                                                       [self.goodsImgView.ubo fw:UB_HALF uh:kHZGoodsColLabelHeight/2])];
        self.goodsPriceLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsPriceLabel setTextColor:HZ_MALL_MAIN_THEME_ORANGE];
        self.goodsPriceLabel.textAlignment = NSTextAlignmentLeft;
        [self.goodsPriceLabel setFont:kHZRFont_Menu_S(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        [self addSubview:self.goodsPriceLabel];
        
        //标签
        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPriceLabel.ufo fx:UB_RIGHT ux:-kHZGoodsColViewMargin fy:UB_TOP],
                                                                       [self.goodsPriceLabel.ubo fw:UB_WIDTH uw:-4*kHZGoodsColViewMargin fh:UB_HEIGHT])];
        self.tagLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.tagLabel setTextColor:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR];
        self.tagLabel.textAlignment = NSTextAlignmentRight;
        [self.tagLabel setFont:kHZRFont_Tag_M(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
//        self.tagLabel.layer.masksToBounds = YES;
//        self.tagLabel.layer.cornerRadius = [self.ubo uw:4];
//        self.tagLabel.layer.borderWidth = [self.ubo pw:1];
//        self.tagLabel.layer.borderColor = HZ_MALL_SEPARATOR_CELL_LINE.CGColor;
        self.tagLabel.userInteractionEnabled = YES;
        [self addSubview:self.tagLabel];
        
        //特卖Label
        self.temaiLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPriceLabel.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kSizeMallViewMargin/2],
                                                                  [self.goodsTitleLabel.ubo fw:UB_WIDTH uh:kHZGoodsViewTeMaiBtnHeight])];
        self.temaiLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.temaiLabel setTextColor:HZ_CLEAR_COLOR];
        self.temaiLabel.textAlignment = NSTextAlignmentCenter;
        [self.temaiLabel setFont:kHZRFont_Content_S(kHZGoodsTabFontRateSix,kHZGoodsTabFontRatePlus)];
        self.temaiLabel.layer.masksToBounds = YES;
        self.temaiLabel.layer.cornerRadius = [self.ubo uw:4];
        [self addSubview:self.temaiLabel];
        
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagLabelOnTouch)];
        [self.tagLabel addGestureRecognizer:tapGes];
        
    }
    return self;
}

- (void)refreshViewWithModel:(HZGoodsModel *)model timeoffset:(NSTimeInterval)timeoffset{
    self.model = model;
    [self reloadTeMailLabelView:timeoffset];
}

- (void)setModel:(HZGoodsModel *)model{
    _model = model;
    
    //[self.goodsImgView setImageWithURL:model.picURL];
    
    //图像的动画特效
    [self.goodsImgView sd_setImageWithURL:[NSURL URLWithString:model.picURL] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.goodsImgView.alpha = 0.0f;
        [UIView animateWithDuration:0.5f animations:^{
            self.goodsImgView.alpha = 1.0f;
        }];
    }];
    
    self.goodsTitleLabel.text = model.productTitle;
    
    self.goodsPriceLabel.text = [NSString stringWithFormat:@"￥%@", model.salePrice];
    if(model.keywords.length){
        self.tagLabel.hidden = NO;
        self.tagLabel.text = model.keywords;
    }else{
        self.tagLabel.hidden = YES;
        self.tagLabel.text = @"";
    }
    
    if (model.modelType == HZGoodsModelTypeNormal) {
        self.temaiLabel.frame = CGRectOf([self.goodsPriceLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                         [self.goodsTitleLabel.ubo fw:UB_WIDTH uh:0]);
        [self.temaiLabel setTextColor:HZ_CLEAR_COLOR];
        
    }else{
        self.temaiLabel.frame = CGRectOf([self.goodsPriceLabel.ufo fx:UB_LEFT fy:UB_BOTTOM uy:kSizeMallViewMargin/2],
                                         [self.goodsTitleLabel.ubo fw:UB_WIDTH uh:kHZGoodsViewTeMaiBtnHeight]);
        [self.temaiLabel setTextColor:HZ_WHITE_COLOR];
    }
    
    self.temaiLabel.backgroundColor = HZ_CLEAR_COLOR;
    
    self.temaiLabel.text = @"";
    
}

- (void)reloadTeMailLabelView:(NSTimeInterval)offsetInterval{
    NSTimeInterval nowTime = [[[NSDate alloc] init] timeIntervalSince1970];
    
    if (nowTime - offsetInterval < [self.model.srvStartInterval floatValue]) {
        self.temaiLabel.backgroundColor = HZ_MALL_L_LIGHT_GRAY_BACKGROUND_COLOR;
        
        self.temaiLabel.text = @"即将开售";
    }else if(nowTime - offsetInterval >= [self.model.srvStartInterval floatValue] && nowTime - offsetInterval <  [self.model.srvEndInterval floatValue]){
        self.temaiLabel.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
        
        self.temaiLabel.text = @"正在热卖中..";
    }else{
        self.temaiLabel.backgroundColor = HZ_MALL_L_LIGHT_GRAY_BACKGROUND_COLOR;
        
        self.temaiLabel.text = @"已结束";
    }
}

- (void)tagLabelOnTouch{
    if (self.targetCallerTapTag) {
        [self.targetCallerTapTag call:self.tagLabel.text];
    }
}

@end
